﻿using GameFramework;
using UnityEngine;
using UMT.Framework.Managers;
using Yoozoo.Mars.Got;
#if UNITY_EDITOR
using UnityEditor;
#endif

[ExecuteInEditMode]
[RequireComponent(typeof(Camera))]
public class AfterEffectControl : MonoBehaviour
{
    [SerializeField, HideInInspector]
    private Color gradualColor = new Color(0.1647f, 0.3529f, 0.4117f, 1f);

    [SerializeField, HideInInspector]
    private float fGradualStart = 0.1f;

    [SerializeField, HideInInspector]
    private float fGradualEnd = 1.27f;

    [Range(0, 10), SerializeField, HideInInspector]
    private float fGradualExp = 6.55f;

    [SerializeField, HideInInspector]
    private Vector4 vColorAdjustParam = new Vector4(1, 1.1f, 0.08f, 0);

    [Range(0, 2), SerializeField, HideInInspector]
    private Vector2 vignetteIntensity = new Vector2(1, 1);

    [Range(0, 1), SerializeField, HideInInspector]
    private Vector2 vignetteOffest = new Vector2(1, 1);

    [SerializeField, HideInInspector]
    private Color vignetteColor = new Color(0, 0, 0, 1);

    [Range(0, 1)]
    public float grayLerp = 0f;

    public bool enableWhiteBalance;

    [Range(-10, 10), SerializeField, HideInInspector]
    private float Temperature = 0f;

    [Range(-10, 10), SerializeField, HideInInspector]
    private float Tint = 0f;

    private Shader afterEffectShader = null;
    private Material afterEffectMaterial = null;

    public Color _GradualColor
    {
        get
        {
            return gradualColor;
        }

        set
        {
            if (gradualColor != value)
            {
                Shader.SetGlobalColor("_GradualColor", value);
            }
            gradualColor = value;
        }
    }

    public float _fGradualStart
    {
        get
        {
            return fGradualStart;
        }

        set
        {
            if (fGradualStart != value)
            {
                Shader.SetGlobalFloat("_fGradualStart", value);
            }
            fGradualStart = value;
        }
    }

    public float _fGradualEnd
    {
        get
        {
            return fGradualEnd;
        }

        set
        {
            if (fGradualEnd != value)
            {
                Shader.SetGlobalFloat("_fGradualEnd", value);
            }
            fGradualEnd = value;
        }
    }

    public float _fGradualExp
    {
        get
        {
            return fGradualExp;
        }

        set
        {
            if (fGradualExp != value)
            {
                Shader.SetGlobalFloat("_fGradualExp", value);
            }
            fGradualExp = value;
        }
    }

    public Vector4 _vColorAdjustParam
    {
        get
        {
            return vColorAdjustParam;
        }

        set
        {
            if (vColorAdjustParam != value)
            {
                Shader.SetGlobalVector("_vColorAdjustParam", value);
            }
            vColorAdjustParam = value;
        }
    }

    public Vector2 _VignetteIntensity
    {
        get
        {
            return vignetteIntensity;
        }

        set
        {
            if (vignetteIntensity != value)
            {
                Shader.SetGlobalVector("_VignetteIntensity", value);
            }
            vignetteIntensity = value;
        }
    }

    public Vector2 _VignetteOffest
    {
        get
        {
            return vignetteOffest;
        }

        set
        {
            if (vignetteOffest != value)
            {
                Shader.SetGlobalVector("_VignetteOffest", value);
            }
            vignetteOffest = value;
        }
    }

    public Color _VignetteColor
    {
        get
        {
            return vignetteColor;
        }

        set
        {
            if (vignetteColor != value)
            {
                Shader.SetGlobalColor("_VignetteColor", value);
            }
            vignetteColor = value;
        }
    }

    public float _Temperature
    {
        get
        {
            return Temperature;
        }

        set
        {
            if (Temperature != value)
            {
                Shader.SetGlobalFloat("_Temperature", value);
            }
            Temperature = value;
        }
    }

    public float _Tint
    {
        get
        {
            return Tint;
        }

        set
        {
            if (Tint != value)
            {
                Shader.SetGlobalFloat("_Tint", value);
            }
            Tint = value;
        }
    }



    //wwei@yoozoo.com 2020-04-10
    //===================================分辨率降低处理===========================
    #region 视口调整属性


    /// <summary>
    /// 如果需要继续优化采样效率，这里请在相机上选择最近点采样
    /// </summary>
    [SerializeField]
    public FilterMode filterMode = FilterMode.Bilinear;
    
    [SerializeField,Range(0.01F, 1.0F)]
    public float _renderScale = 1.0F;
    
    private Rect _originalRect;
    private Rect _scaledRect;
    
    private Camera _camera;
    
    
    private Camera camera
    {
        get
        {
            _camera = _camera?_camera:GetComponent<Camera>();
            return _camera;
        }
    }
    
    public float Scale
    {
        get { return _renderScale; }
        set
        {
            _renderScale = Mathf.Clamp(value,0.1f,1f);
        }
    }


    #endregion
    //==========================================================================
    
    

    private void OnEnable()
    {
        // if (!afterEffectMaterial)
        // {
        //     if (!afterEffectShader)
        //     {
        //         afterEffectShader = ShaderMgr.Find("ImageEffect/AfterEffect");
        //
        //         if (afterEffectShader)
        //         {
        //             afterEffectMaterial = new Material(afterEffectShader);
        //         }
        //     }
        //     else
        //     {
        //         afterEffectMaterial = new Material(afterEffectShader);
        //     }
        // }
        //
        // Shader.SetGlobalColor("_GradualColor", gradualColor);
        // Shader.SetGlobalFloat("_fGradualStart", _fGradualStart);
        // Shader.SetGlobalFloat("_fGradualEnd", _fGradualEnd);
        // Shader.SetGlobalFloat("_fGradualExp", _fGradualExp);
        // Shader.SetGlobalVector("_vColorAdjustParam", _vColorAdjustParam);
        // Shader.SetGlobalVector("_VignetteIntensity", _VignetteIntensity);
        // Shader.SetGlobalVector("_VignetteOffest", _VignetteOffest);
        // Shader.SetGlobalColor("_VignetteColor", _VignetteColor);
        //
        // if (enableWhiteBalance)
        // {
        //     Shader.SetGlobalFloat("_Temperature", _Temperature);
        //
        //     Shader.SetGlobalFloat("_Tint", _Tint);
        // }
        // else
        // {
        //     Shader.SetGlobalFloat("_Temperature", 0);
        //
        //     Shader.SetGlobalFloat("_Tint", 0);
        // }

        _renderScale = DeviceQuality.Instance.ZoomScale;
        
    }
    
    // Update is called once per frame
    //void Update()
    //{
       // Shader.SetGlobalFloat("_GrayLerp", grayLerp);


    //}

    void OnRenderImage(RenderTexture sourceTexture, RenderTexture destTexture)
    {
        //wwei@yoozoo.com 2020-04-10
        //===================================视口拉回==============================
        //SceneManager.Instance.curScene.sceneType != SceneType.MainCity &&
        if ( _renderScale < 1)
        {
            camera.rect = _originalRect;
            sourceTexture.filterMode = filterMode;
        }
        //==========================================================================
        
        if (afterEffectMaterial == null || afterEffectMaterial.shader == null || afterEffectMaterial.shader.isSupported == false)
        {
            Graphics.Blit(sourceTexture, destTexture);
        }
        else
        {
            Graphics.Blit(sourceTexture, destTexture, afterEffectMaterial);
        }
    }
    
    //wwei@yoozoo.com 2020-04-10
    //===================================视口缩小===========================
    void OnPreRender ()
    {
        //SceneManager.Instance.curScene.sceneType != SceneType.MainCity &&
        if ( _renderScale < 1)
        {
            _originalRect = camera.rect;
            _scaledRect.Set(_originalRect.x, _originalRect.y, _originalRect.width * _renderScale,
                _originalRect.height * _renderScale);
            camera.rect = _scaledRect;
        }
    }
    //==========================================================================
}


#if UNITY_EDITOR
// Creates a custom Label on the inspector for all the scripts named ScriptName
// Make sure you have a ScriptName script in your
// project, else this will not work.
[CustomEditor(typeof(AfterEffectControl))]
public class AfterEffectControlEditor : Editor
{
    private AfterEffectControl m_AfterEffectControl = null;

    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();

        m_AfterEffectControl = target as AfterEffectControl;
        GUILayout.Label("后处理编辑器");
        EditorGUI.BeginChangeCheck();

        m_AfterEffectControl._GradualColor = EditorGUILayout.ColorField("渐变颜色", m_AfterEffectControl._GradualColor);
        m_AfterEffectControl._fGradualStart = EditorGUILayout.FloatField("渐变起始位置", m_AfterEffectControl._fGradualStart);
        m_AfterEffectControl._fGradualEnd = EditorGUILayout.FloatField("渐变结束位置", m_AfterEffectControl._fGradualEnd);
        m_AfterEffectControl._fGradualExp = EditorGUILayout.Slider("渐变范围", m_AfterEffectControl._fGradualExp, 0, 10f);
        m_AfterEffectControl._vColorAdjustParam = EditorGUILayout.Vector4Field("亮度(0,2)/对比度(0.5,1.5)/饱和度(-2,3)", m_AfterEffectControl._vColorAdjustParam);
        m_AfterEffectControl._VignetteIntensity = EditorGUILayout.Vector2Field("暗角强度", m_AfterEffectControl._VignetteIntensity);
        m_AfterEffectControl._VignetteOffest = EditorGUILayout.Vector2Field("暗角渐变", m_AfterEffectControl._VignetteOffest);
        m_AfterEffectControl._VignetteColor = EditorGUILayout.ColorField("暗角颜色", m_AfterEffectControl._VignetteColor);

        if (m_AfterEffectControl.enableWhiteBalance)
        {
            m_AfterEffectControl._Temperature = EditorGUILayout.Slider("色温", m_AfterEffectControl._Temperature, -1, 1f);
            m_AfterEffectControl._Tint = EditorGUILayout.Slider("色温偏移", m_AfterEffectControl._Tint, -1, 1f);
        }


        if (EditorGUI.EndChangeCheck())
        {
            Undo.RecordObject(m_AfterEffectControl, "Undo");
            EditorUtility.SetDirty(m_AfterEffectControl);
        }
    }
}

#endif
